﻿//using Beginor.Owin.StaticFile;
using Microsoft.AspNet.SignalR;
using Microsoft.Owin;
using Microsoft.Owin.Cors;
using Owin;
using System;
using System.Net.Http.Headers;
using System.Web.Http;
using System.Web.Http.Cors;

[assembly: OwinStartup(typeof(RDSEngine.Startup))]
namespace RDSEngine
{
    public class Startup
    {
        // This code configures Web API. The Startup class is specified as a type
        // parameter in the WebApp.Start method.
        public void Configuration(IAppBuilder appBuilder)
        {
            // Configure Web API for self-host. 
            HttpConfiguration config = new HttpConfiguration();
            config.Formatters.XmlFormatter.SupportedMediaTypes.Clear();
            config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
            config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/plain"));
            config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/json"));
            //config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/form-data"));
            config.EnableCors(new EnableCorsAttribute("*", "*", "*"));//解决跨域问题，需要nuget Cors

            //config.Services
            //config.Filters.Add();

            //启用标记路由
            config.MapHttpAttributeRoutes();
            //默认的Web API路由
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                //routeTemplate: "api/{controller}/{action}/{id}", //{action}目的是为了一个Controller能有多个Get Post方法
                routeTemplate: "api/{controller}/{id}",
                //defaults: new { id = RouteParameter.Optional, action = RouteParameter.Optional } //默认Action和默认id
                defaults: new { id = RouteParameter.Optional }
                );  

            //将路由配置附加到appBuilder
            appBuilder.UseWebApi(config);
            //appBuilder.us

            //静态文件托管
            //appBuilder.Map("/page", map =>
            //{
            //    // 允许跨域
            //    map.UseCors(CorsOptions.AllowAll);
            //    map.UseStaticFile(new StaticFileMiddlewareOptions
            //    {
            //        RootDirectory = @"../Debug/page",
            //        DefaultFile = "page/index.html",
            //        EnableETag = true,
            //        EnableHtml5LocationMode = true,
            //        MimeTypeProvider = new MimeTypeProvider(new Dictionary<string, string>
            //        {
            //            { ".html", "text/html" },
            //            { ".htm", "text/html" },
            //            { ".dtd", "text/xml" },
            //            { ".xml", "text/xml" },
            //            { ".ico", "image/x-icon" },
            //            { ".css", "text/css" },
            //            { ".js", "application/javascript" },
            //            { ".json", "application/json" },
            //            { ".jpg", "image/jpeg" },
            //            { ".png", "image/png" },
            //            { ".gif", "image/gif" },
            //            { ".config", "text/xml" },
            //            { ".woff2", "application/font-woff2"},
            //            { ".eot", "application/vnd.ms-fontobject" },
            //            { ".svg", "image/svg+xml" },
            //            { ".woff", "font/x-woff" },
            //            { ".txt", "text/plain" },
            //            { ".log", "text/plain" }
            //        })
            //    });
            //});

            //SignalR托管
            appBuilder.Map("/signalr", map =>
            {
                // 允许跨域
                map.UseCors(CorsOptions.AllowAll);

                var hubConfiguration = new HubConfiguration
                {
                    //Resolver = **,
                    // You can enable JSONP by uncommenting line below.
                    // JSONP requests are insecure but some older browsers (and some
                    // versions of IE) require JSONP to work cross domain
                    EnableJSONP = true,
                    EnableDetailedErrors = true
                };

                // Run the SignalR pipeline. We‘re not using MapSignalR
                // since this branch is already runs under the "/signalr"
                // path.
                map.RunSignalR(hubConfiguration);
            });

            //该值表示连接在超时之前保持打开状态的时间长度。
            //默认为110秒
            GlobalHost.Configuration.ConnectionTimeout = TimeSpan.FromSeconds(110);

            //该值表示在连接停止之后引发断开连接事件之前要等待的时间长度。
            //默认为30秒
            GlobalHost.Configuration.DisconnectTimeout = TimeSpan.FromSeconds(60);
        }
    }
}
